from nicegui import persistence, ui, app
from db.bs_crud import create_lte_freq_record
from db.schemas.bs_mode_schemas import ILTEFreqRecordCreate
from sqlalchemy.orm import Session


class AddLteFreqRecordDialog:
    def __init__(self, db: Session, base_station_id: int) -> None:
        """
        自定义增加 LTE 频率记录 Dialog
        """
        self.db = db
        self.base_station_id = base_station_id
        # 使用 model_construct() 创建（跳过pydantic验证）
        self.lte_freq_record = ILTEFreqRecordCreate.model_construct()
        self.lte_freq_record.base_station_id = base_station_id

    def show(self):
        with ui.dialog().classes("w-[800px] h-[900px] bg-sky-600") as dialog, ui.card():
            with ui.card_section():
                ui.label("添加 LTE 频率记录").classes("text-h6 w-[300px]")
            with ui.card_section().classes("q-pt-none"):
                with ui.column().classes("w-full"):
                    # 点位编号
                    ui.input("1.点位编号").bind_value(
                        self.lte_freq_record, "point_number"
                    )
                    # 与天线最小高度差
                    ui.number(placeholder="2.与天线最小高度差（m）").bind_value(
                        self.lte_freq_record, "line_min"
                    )
                    # 监测结果1
                    ui.number(placeholder="3.监测结果1(V/m)").bind_value(
                        self.lte_freq_record, "result_1"
                    )
                    # 监测结果2
                    ui.number(placeholder="4.监测结果2(V/m)").bind_value(
                        self.lte_freq_record, "result_2"
                    )
                    # 监测结果3
                    ui.number(placeholder="5.监测结果3(V/m)").bind_value(
                        self.lte_freq_record, "result_3"
                    )
                    # 监测结果4
                    ui.number(placeholder="6.监测结果4(V/m)").bind_value(
                        self.lte_freq_record, "result_4"
                    )
                    # 监测结果5
                    ui.number(placeholder="7.监测结果5(V/m)").bind_value(
                        self.lte_freq_record, "result_5"
                    )
                    # 点位描述
                    ui.input("8.点位描述").bind_value(
                        self.lte_freq_record, "point_description"
                    )
                    # 经度
                    ui.number(precision=6, placeholder="9.经度(°)").bind_value(
                        self.lte_freq_record, "longitude"
                    )
                    # 纬度
                    ui.number(precision=6, placeholder="10.纬度(°)").bind_value(
                        self.lte_freq_record, "latitude"
                    )
                    # 备注信息
                    ui.input("11.备注信息").bind_value(self.lte_freq_record, "remarks")
                    # 与天线最小水平距离
                    ui.number(placeholder="12.与天线最小水平距离（m）").bind_value(
                        self.lte_freq_record, "line_max"
                    )
                    # 检测人
                    ui.input("13.检测人").bind_value(self.lte_freq_record, "recorder")
            with ui.card_actions().classes("w-full justify-end"):
                ui.button("创建记录", on_click=lambda: self.add_record()).props("flat")
                ui.button("取消", on_click=dialog.close).props("flat")
        dialog.open()

    def add_record(self):
        if self.lte_freq_record.point_number:
            try:
                create_lte_freq_record(self.db, self.lte_freq_record)
                ui.notify("LTE 频率记录创建成功!", position="center", type="positive")
            except Exception as e:
                ui.notify(
                    f"LTE 频率记录创建失败: {str(e)}",
                    position="center",
                    type="negative",
                )
        else:
            ui.notify("点位编号不能为空!", position="center", type="warning")
